public class L977 {

    public static void main(String[] args) {

        int[] nums = {-4, -1, 0, 3, 10};
        int[] ints = new Solution().sortedSquares(nums);
        System.out.println(ints);


    }

    static class Solution {
        public int[] sortedSquares(int[] nums) {
            int[] newNums = new int[nums.length];
            int i = 0;
            int j = nums.length - 1;
            int newIndex = nums.length - 1;

            while (newIndex >= 0) {
                if (nums[i] * nums[i] <= nums[j] * nums[j]) {
                    newNums[newIndex] = nums[j] * nums[j];
                    j--;
                } else {
                    newNums[newIndex] = nums[i] * nums[i];
                    i++;
                }
                newIndex--;
            }
            return newNums;
        }
    }

}

